package ocean.chinacraft.common.world.item.decoration;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import ocean.chinacraft.common.ModEntityTypes;
import ocean.chinacraft.common.config.GroupDefine;
import ocean.chinacraft.common.entity.fly.SkyLanternEntity;

/**
 * 孔明灯
 *
 * @author zhang
 * @since 2021/12/29
 */
public class SkyLantern extends Item {

    public SkyLantern() {
        super(new Properties().tab(GroupDefine.CHINACRAFT_DECORATE));
    }

    @Override
    public ActionResultType useOn(ItemUseContext context) {
        World level = context.getLevel();
        if (!level.isClientSide && context.getPlayer() != null) {
            ItemStack itemStack = context.getItemInHand();
            PlayerEntity player = context.getPlayer();
            SkyLanternEntity skyLantern = new SkyLanternEntity(ModEntityTypes.SKY_LANTERN.get(), level);
            Vector3d location = context.getClickLocation();
            skyLantern.setPos(location.x(), location.y(), location.z());
            level.addFreshEntity(skyLantern);
            if (!player.abilities.instabuild) {
                itemStack.shrink(1);
            }
            return ActionResultType.SUCCESS;
        }
        return super.useOn(context);
    }
}
